package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 消息媒体文件VO - 遵循单一职责原则
 * 只负责消息媒体文件信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "消息媒体文件VO")
public class ChatMessageMediaVO {

    @Schema(description = "媒体文件ID")
    private String id;

    @Schema(description = "消息ID")
    private String messageId;

    @Schema(description = "媒体类型")
    private String mediaType;

    @Schema(description = "文件名")
    private String fileName;

    @Schema(description = "文件路径")
    private String filePath;

    @Schema(description = "文件URL")
    private String fileUrl;

    @Schema(description = "文件大小（字节）")
    private Long fileSize;

    @Schema(description = "文件扩展名")
    private String fileExtension;

    @Schema(description = "MIME类型")
    private String mimeType;

    @Schema(description = "缩略图路径")
    private String thumbnailPath;

    @Schema(description = "缩略图URL")
    private String thumbnailUrl;

    @Schema(description = "媒体宽度（图片/视频）")
    private Integer width;

    @Schema(description = "媒体高度（图片/视频）")
    private Integer height;

    @Schema(description = "媒体时长（音频/视频，秒）")
    private Integer duration;

    @Schema(description = "媒体描述")
    private String description;

    @Schema(description = "访问权限")
    private String accessLevel;

    @Schema(description = "是否压缩")
    private Boolean isCompressed;

    @Schema(description = "压缩质量")
    private Integer compressionQuality;

    @Schema(description = "扩展信息")
    private String extraInfo;

    @Schema(description = "上传者ID")
    private String uploaderId;

    @Schema(description = "上传者名称")
    private String uploaderName;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "下载次数")
    private Integer downloadCount;

    @Schema(description = "查看次数")
    private Integer viewCount;

    /**
     * 检查是否为图片类型
     * 
     * @return 是否为图片
     */
    public boolean isImage() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("image");
    }

    /**
     * 检查是否为视频类型
     * 
     * @return 是否为视频
     */
    public boolean isVideo() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("video");
    }

    /**
     * 检查是否为音频类型
     * 
     * @return 是否为音频
     */
    public boolean isAudio() {
        if (mediaType == null) {
            return false;
        }
        return mediaType.toLowerCase().startsWith("audio");
    }

    /**
     * 检查是否为文档类型
     * 
     * @return 是否为文档
     */
    public boolean isDocument() {
        if (mediaType == null) {
            return false;
        }
        String type = mediaType.toLowerCase();
        return type.contains("pdf") || type.contains("doc") || type.contains("txt") ||
               type.contains("excel") || type.contains("ppt");
    }

    /**
     * 获取文件大小的可读格式
     * 
     * @return 可读的文件大小
     */
    public String getReadableFileSize() {
        if (fileSize == null || fileSize == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double size = fileSize.doubleValue();
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return String.format("%.1f %s", size, units[unitIndex]);
    }

    /**
     * 获取媒体类型的显示名称
     * 
     * @return 媒体类型显示名称
     */
    public String getMediaTypeDisplayName() {
        if (isImage()) {
            return "图片";
        } else if (isVideo()) {
            return "视频";
        } else if (isAudio()) {
            return "音频";
        } else if (isDocument()) {
            return "文档";
        } else {
            return "文件";
        }
    }

    /**
     * 获取媒体类型图标
     * 
     * @return 媒体类型图标
     */
    public String getMediaTypeIcon() {
        if (isImage()) {
            return "icon-image";
        } else if (isVideo()) {
            return "icon-video";
        } else if (isAudio()) {
            return "icon-audio";
        } else if (isDocument()) {
            return "icon-document";
        } else {
            return "icon-file";
        }
    }

    /**
     * 检查是否有缩略图
     * 
     * @return 是否有缩略图
     */
    public boolean hasThumbnail() {
        return (thumbnailPath != null && !thumbnailPath.trim().isEmpty()) ||
               (thumbnailUrl != null && !thumbnailUrl.trim().isEmpty());
    }

    /**
     * 获取显示的缩略图URL
     * 
     * @return 缩略图URL
     */
    public String getDisplayThumbnailUrl() {
        if (thumbnailUrl != null && !thumbnailUrl.trim().isEmpty()) {
            return thumbnailUrl;
        }
        if (isImage() && fileUrl != null) {
            return fileUrl; // 图片可以直接使用原图作为缩略图
        }
        return getDefaultThumbnail();
    }

    /**
     * 获取默认缩略图
     * 
     * @return 默认缩略图URL
     */
    public String getDefaultThumbnail() {
        if (isImage()) {
            return "/images/default-image-thumbnail.png";
        } else if (isVideo()) {
            return "/images/default-video-thumbnail.png";
        } else if (isAudio()) {
            return "/images/default-audio-thumbnail.png";
        } else if (isDocument()) {
            return "/images/default-document-thumbnail.png";
        } else {
            return "/images/default-file-thumbnail.png";
        }
    }

    /**
     * 检查是否有尺寸信息
     * 
     * @return 是否有尺寸信息
     */
    public boolean hasDimensions() {
        return width != null && height != null && width > 0 && height > 0;
    }

    /**
     * 检查是否有时长信息
     * 
     * @return 是否有时长信息
     */
    public boolean hasDuration() {
        return duration != null && duration > 0;
    }

    /**
     * 获取时长的可读格式
     * 
     * @return 可读的时长
     */
    public String getReadableDuration() {
        if (!hasDuration()) {
            return "00:00";
        }
        
        int minutes = duration / 60;
        int seconds = duration % 60;
        
        if (minutes >= 60) {
            int hours = minutes / 60;
            minutes = minutes % 60;
            return String.format("%02d:%02d:%02d", hours, minutes, seconds);
        } else {
            return String.format("%02d:%02d", minutes, seconds);
        }
    }

    /**
     * 获取尺寸的显示格式
     * 
     * @return 尺寸显示格式
     */
    public String getDimensionDisplay() {
        if (!hasDimensions()) {
            return "";
        }
        return width + "×" + height;
    }

    /**
     * 检查是否被压缩
     * 
     * @return 是否被压缩
     */
    public boolean isCompressed() {
        return isCompressed != null && isCompressed;
    }

    /**
     * 获取压缩质量的显示名称
     * 
     * @return 压缩质量显示名称
     */
    public String getCompressionQualityName() {
        if (compressionQuality == null) {
            return "原始";
        }
        if (compressionQuality >= 90) {
            return "高质量";
        } else if (compressionQuality >= 70) {
            return "中等质量";
        } else {
            return "低质量";
        }
    }

    /**
     * 获取访问权限的显示名称
     * 
     * @return 访问权限显示名称
     */
    public String getAccessLevelName() {
        if (accessLevel == null) {
            return "公开";
        }
        switch (accessLevel.toLowerCase()) {
            case "private":
                return "私有";
            case "group":
                return "群组内";
            case "friend":
                return "好友";
            default:
                return "公开";
        }
    }

    /**
     * 检查是否可以预览
     * 
     * @return 是否可以预览
     */
    public boolean canPreview() {
        return isImage() || isVideo() || isAudio() || 
               (isDocument() && (mimeType != null && mimeType.contains("pdf")));
    }

    /**
     * 检查是否可以在线播放
     * 
     * @return 是否可以在线播放
     */
    public boolean canPlayOnline() {
        return isVideo() || isAudio();
    }

    /**
     * 获取文件信息摘要
     * 
     * @return 文件信息摘要
     */
    public String getFileSummary() {
        StringBuilder summary = new StringBuilder();
        summary.append(getMediaTypeDisplayName());
        
        if (hasDimensions()) {
            summary.append(" ").append(getDimensionDisplay());
        }
        
        if (hasDuration()) {
            summary.append(" ").append(getReadableDuration());
        }
        
        summary.append(" ").append(getReadableFileSize());
        
        return summary.toString();
    }
}
